package elcs.member.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import elcs.board.model.ListVO;
import elcs.board.model.PagingBean;
import elcs.group.model.GroupDao;
import elcs.group.model.GroupVO;
import elcs.point.model.TradePointDao;
import elcs.point.model.TradePointVO;
@Repository
public class MemberServiceImpl implements MemberService{
	@Resource(name="memberDaoImpl")
	private MemberDao memberDao;
	@Resource(name="tradePointDaoImpl")
	private TradePointDao tradePointDao;
	@Resource(name="groupDaoImpl")
	private GroupDao groupDao;
	

	@Override
	public MemberVO login(HttpSession session, MemberVO mvo) {
		// TODO Auto-generated method stub
		mvo=memberDao.findMemberByIdAndPassword(mvo);
		if(mvo!=null)
		{
			System.out.println("로그인되는 "+mvo);
			session.setAttribute("mvo", mvo);
		}
		return mvo;
	}

	@Override
	public void logout(HttpSession session) {
		// TODO Auto-generated method stub
		session.invalidate();
		
	}

	@Override
	public boolean checkMemberById(String id) {
		// TODO Auto-generated method stub
		MemberVO mvo=memberDao.findMemberById(id);
		System.out.println(mvo);
		if(mvo==null){
			return true;
		}
		return false;
	}

	@Override
	public boolean checkMemberByNick(String nick) {
		// TODO Auto-generated method stub
		MemberVO mvo=memberDao.findMemberByNick(nick);
		if(mvo==null){
			return true;
		}
		
		return false;
	}

	@Transactional
	@Override
	public void register(MemberVO mvo) {
		// TODO Auto-generated method stub
		memberDao.register(mvo);
		TradePointVO tpVO=new TradePointVO();
		tpVO.setContent("회원등록");
		tpVO.setPlusPoint(3000);
		tpVO.setId(mvo.getId());
		tradePointDao.register(tpVO);
	}

	@Override
	public ListVO findAllMember(HttpServletRequest request) throws Exception {
		String pageNo=request.getParameter("pageNo");
		if(pageNo==null||pageNo=="") 
			pageNo="1";
		List list=memberDao.findAllMember(pageNo);
		int total=memberDao.totalContent();
		PagingBean paging=new PagingBean(total,Integer.parseInt(pageNo));
		ListVO lvo=new ListVO(list,paging);
		return lvo;
	}

	//1213csc
	@Override
	public MemberVO findByMemberId(String id) {
		// TODO Auto-generated method stub
		
		return memberDao.findMemberById(id);
	}

	@Override
	public void updateMemberByLevelPoint(MemberVO mvo) {
		// TODO Auto-generated method stub
		memberDao.updateMemberByLevelPoint(mvo);
		
		
	}

	@Override
	public ListVO manageFindByMethodAllList(HttpServletRequest request,
			String method, String value) throws Exception {
		String pageNo=request.getParameter("pageNo");
		System.out.println(value);
		if(pageNo==null||pageNo=="") 
			pageNo="1";
		List list=null;
		if(method.equals("level"))
		{
			list=memberDao.findMemberByLevel(value);
		}else if(method.equals("id"))
		{
			list= new ArrayList();
			list.add(memberDao.findMemberById(value));
		}else if(method.equals("name"))
		{
			list=memberDao.findMemberByName(value);
		}else if(method.equals("address"))
		{
			list=memberDao.findMemberByAddress(value);
		}
		else 
		{
			list= new ArrayList();
			list.add(memberDao.findMemberByNick(value));
		}
		int total=list.size();
		PagingBean paging=new PagingBean(total,Integer.parseInt(pageNo));
		ListVO lvo=new ListVO(list,paging);
		return lvo;
	}
	
	@Override
	public Map suchMember(String method, MemberVO mvo) {
		// TODO Auto-generated method stub
		MemberVO resultVO=null;
		Map map= new HashMap();
		if(method.equals("id"))
		{
			resultVO=memberDao.findMemberBySsn(mvo.getSsn());
			
		}
		else
		{
			
			resultVO=memberDao.findMemberByIdAndSsn(mvo);
		}
		if(resultVO==null)
		{
			map.put("flag", false);
		}
		else
		{
			map.put("flag", true);
			map.put("mvo", resultVO);
			
		}
		return map;
		
	}

	@Override
	public boolean updateMemberByPassword(MemberVO mvo) {
		// TODO Auto-generated method stub
		if(memberDao.updateMemberByPassword(mvo)==0)
		{
			return false;
		}
		return true;
	}
	
	@Override
	public List<MemberVO> mypageMyInfo(HttpSession session) {
		// TODO Auto-generated method stub
		return memberDao.mypageMyInfo();
	}

	@Override
	public void mypageUpdate(MemberVO mvo) {
		// TODO Auto-generated method stub
		memberDao.mypageUpdate(mvo);
	}
	
	@Override
	public void mypageDelete(MemberVO mvo) {
		// TODO Auto-generated method stub
		memberDao.mypageDelete(mvo);
	}

	@Override
	public MemberVO findByMemberSsn(int ssn) {
		// TODO Auto-generated method stub
		return (MemberVO) memberDao.findMemberBySsn(ssn);
		
	}

	@Override
	public boolean checkMemberBySsn(String ssn) {
		// TODO Auto-generated method stub
		int iSsn =Integer.parseInt(ssn);
		MemberVO mvo=memberDao.findMemberBySsn(iSsn);
		if(mvo==null){
			return true;
		}
		
		return false;
	}

	@Override
	public List findRankList() {
		// TODO Auto-generated method stub
		return memberDao.findRankList();
	}

	@Override
	public void updateMemberByGroup(HttpSession session,String gname) {
		// TODO Auto-generated method stub
		MemberVO mvo = (MemberVO)session.getAttribute("mvo");
		mvo.setGvo(groupDao.findByGname(gname));
		memberDao.updateMemberByGroup(mvo);
		session.setAttribute("mvo", mvo);
		
		
	}

	@Override
	public List findByMemberGroup(String gname) {
		// TODO Auto-generated method stub
		return memberDao.findmemberByGroup(gname);
	}

	@Override
	public void groupBan(HttpSession session,String id) {
		// TODO Auto-generated method stub
		MemberVO mvo =new MemberVO();
		mvo.setId(id);
		mvo.setGvo(new GroupVO());
		mvo.getGvo().setGname("");
		System.out.println(mvo);
		memberDao.updateMemberByGroup(mvo);
		
	}

}
